package com.wzqj.agent.util;

import java.util.Random;

/**
 * 验证码工具类生成验证码
 * Created by wanghao@weipass.cn on 2015/7/30.
 */
public class CaptchaUtil {

    public static String createCaptcha(){

        StringBuffer captcha = new StringBuffer();
        Random random = new Random();
        captcha.append(random.nextInt(10)).append(random.nextInt(10))//
            .append(random.nextInt(10)).append(random.nextInt(10));
        return captcha.toString();
    }

    /**
     * 生成随机密码
     * @param length
     * @return
     */
    public static String generatPassword(int length) {

        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int num = random.nextInt(62);
            buf.append(str.charAt(num));
        }
        return buf.toString();
    }

    public static void main(String[] args) {

        System.out.printf(generatPassword(5));
        System.out.printf(createCaptcha());
    }
}
